package com.beemans.corekit.integration.admob;

import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;

import com.google.android.gms.ads.AdValue;
import com.google.android.gms.ads.ResponseInfo;

public class AbBannerAdvert extends AbBaseAdvert implements AbAdNetworkListener, AbAdEventListener {
    @NonNull
    private final AbBannerAdvertConfig advertConfig;
    @Nullable
    private AbBannerAdvertCallback advertCallback;
    @Nullable
    private AbBannerAdWrapper adWrapper;

    public AbBannerAdvert(
            @NonNull LifecycleOwner lifecycleOwner,
            @NonNull ViewGroup advertContainer,
            @NonNull AbBannerAdvertConfig advertConfig,
            @Nullable AbBannerAdvertCallback advertCallback
    ) {
        this.advertConfig = advertConfig;
        this.advertCallback = advertCallback;

        final Context context = AdvertLifeUtils.getContext(lifecycleOwner);
        if (context == null) {
            return;
        }

        adWrapper = new AbBannerAdWrapper(advertContainer, advertConfig);

        AdvertLifeUtils.launchWhenDestroy(lifecycleOwner, this::onAdvertRelease);

        if (advertCallback != null) {
            advertCallback.onAdvertStart(this);
        }
    }

    @Override
    public boolean isAdvertReady() {
        if (adWrapper == null) {
            return false;
        }
        return adWrapper.isAdvertReady();
    }

    @Override
    public boolean isAdvertLoading() {
        if (adWrapper == null) {
            return false;
        }
        return adWrapper.isAdvertLoading();
    }

    @Override
    protected void loadAdvert() {
        if (adWrapper != null) {
            onAdvertRequestPre();

            adWrapper.loadAd(this, this);
        }
    }

    @Override
    public void onAdvertRelease() {
        super.onAdvertRelease();

        if (advertCallback != null) {
            advertCallback = null;
        }
        if (adWrapper != null) {
            adWrapper.destroy();
            adWrapper = null;
        }
    }

    private void onAdvertRequestPre() {
        if (advertCallback != null) {
            final ATAdInfoExt adInfoExt = new ATAdInfoExt();
            adInfoExt.adunit_id = advertConfig.getPlacementId();
            adInfoExt.adunit_format = AdUnitFormat.NATIVE.getValue();
            advertCallback.onAdvertRequestPre(this, adInfoExt);
        }
    }

    private void onAdvertRequestAlt() {
        if (advertCallback != null) {
            final ATAdInfoExt adInfoExt = new ATAdInfoExt();
            adInfoExt.adunit_id = advertConfig.getPlacementId();
            adInfoExt.adunit_format = AdUnitFormat.NATIVE.getValue();
            advertCallback.onAdvertRequestAlt(this, adInfoExt);
        }
    }

    /**
     * @noinspection SameParameterValue
     */
    private void onAdvertLoaded(boolean adLoadedReal) {
        if (adLoadedReal) {
            onAdvertRequestAlt();
        }
        if (advertCallback != null) {
            advertCallback.onAdvertLoaded(this);
        }
    }

    @Override
    public void onAdLoaded() {
        onAdvertLoaded(true);
    }

    @Override
    public void onAdFailedToLoad(@Nullable String loadAdError) {
        if (advertCallback != null) {
            advertCallback.onAdvertLoadFail(this, loadAdError);
        }
    }

    @Override
    public void onAdImpression(@Nullable ResponseInfo responseInfo) {
        if (advertCallback != null) {
            advertCallback.onAdvertShow(this, responseInfo);
        }
    }

    @Override
    public void onAdDisplayFailed(@Nullable String adError) {
        if (advertCallback != null) {
            advertCallback.onAdvertShowFail(this);
        }
    }

    @Override
    public void onAdClicked(@Nullable ResponseInfo responseInfo) {
        if (advertCallback != null) {
            advertCallback.onAdvertClicked(this, responseInfo);
        }
    }

    @Override
    public void onAdClosed(@Nullable ResponseInfo responseInfo) {
        if (advertCallback != null) {
            advertCallback.onAdvertClose(this, responseInfo);
        }
    }

    @Override
    public void onAdPaidEvent(@Nullable ResponseInfo responseInfo, @NonNull AdValue adValue) {
        if (advertCallback != null) {
            advertCallback.onAdvertRevenue(this, responseInfo, adValue);
        }
    }
}